package com.treasure.web.command;

import com.github.pagehelper.PageInfo;
import com.treasure.common.model.PageResult;
import lombok.experimental.UtilityClass;

import java.util.Collections;
import java.util.List;

/**
 * @author sunhongmin
 * @date 2023/11/5 20:10
 * @description 公共分页转换器
 */
@UtilityClass
public class PageConvertor {

    public static <T, R> PageResult<R> convertPage(PageInfo<T> pageInfo, List<R> records) {
        PageResult<R> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setRecords(records);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setSize(pageInfo.getSize());
        pageResult.setCurrent(pageInfo.getPageNum());
        return pageResult;
    }

    public static <R> PageResult<R> emptyPage() {
        PageResult<R> pageResult = new PageResult<>();
        pageResult.setRecords(Collections.emptyList());
        pageResult.setTotal(0);
        return pageResult;
    }
}
